package com.example.win.android_banduyujia.venuesactivity.activity.payactivity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.win.android_banduyujia.R;
import com.example.win.android_banduyujia.http.request.SavePayRequest;
import com.example.win.android_banduyujia.http.response.BaseHttpResponse;
import com.example.win.android_banduyujia.lib.util.StrUtils;
import com.example.win.android_banduyujia.peopleactivity.ui.activity.BaseTitleBarActivity;

/**
 * Created by Win on 2015/9/14.
 */
public class PayActivity extends BaseTitleBarActivity implements View.OnClickListener {

    private EditText  mEditPayName;
    private ImageView mImageViewDelete;
    private TextView  mTxtBound;

    private String    mPayName;
    private String    mToken;
    private String    mCode;
    private String    mRealname;
    private String    mAccount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_pay_page);

        InitTitles();
        InitViews();
    }

    private void InitTitles() {
        showLeftBackButton();
        setTitle(R.string.text_view_roll_out_pay_treasure);
        setRightTextViewListener(R.string.layout_data_save_button, mRightListener);
    }

    private View.OnClickListener  mRightListener = new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            if(StrUtils.isEmpty(mPayName)){
                showToast(R.string.roll_out_pay_treasure);
                return;
            }
            savePayName();
        }
    };

    private Handler  mPayHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            showToast(R.string.bind_success);
            setResult(Activity.RESULT_OK);
            finish();
        }
    };

    private void savePayName() {
        if(savename()) {
            SavePayRequest request = new SavePayRequest(mToken, mCode, mRealname, mAccount);
            mAsycHttpRequest.doRequest(this, request, BaseHttpResponse.class, mPayHandler, true);
        }
    }

    private boolean savename() {
        /*if(StrUtils.isEmpty(mToken)){
            showToast(R.string.pay_no);
            return false;
        }*/
        if(StrUtils.isEmpty(mAccount) || mAccount.isEmpty()){
            showToast(R.string.pay_no_ok);
            return false;
        }
        if(StrUtils.isEmpty(mAccount)){
            showToast(R.string.pay_no_yong);
            return false;
        }
        return true;
    }

    private void InitViews() {
        mTxtBound = (TextView)findViewById(R.id.text_view_band_ding);

        mTxtBound.setOnClickListener(this);

        mEditPayName = (EditText)findViewById(R.id.pay_edit_text);
        mImageViewDelete = (ImageView)findViewById(R.id.pay_image_icon);
        mImageViewDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEditPayName.setText(" ");
            }
        });
        mEditPayName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                mPayName = s.toString();
                if(StrUtils.isEmpty(mPayName)){
                    mImageViewDelete.setVisibility(View.GONE);
                }else{
                    mImageViewDelete.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.text_view_band_ding:

                break;
        }
    }
}
